<?php
namespace app\api\controller;

use app\api\model\AttachFile;
use app\api\model\DevOrder;
use app\api\model\DevOrderPack;
use think\Controller;
use think\Request;
use app\api\validate\CommonValidate;
class Index extends ApiBaseController{
    
    
    private $attachFile;
    private $devOrderPack;
    
    public function __construct(){
        parent::__construct();
        $this->attachFile=new AttachFile();
        $this->devOrderPack=new DevOrderPack();
    }
    
    
    public function index(){
        $test= $this->attachFile->where('id',1)->column('id');
        $this->assign('honors',array("ddd"=>"ddd"));
        return $this->fetch();
    }
    
    /**
     * 接口文档
     */
    public function doc(){
        return $this->fetch();
    }



    public function hello(){
        //$server_url = $_SERVER['SERVER_NAME']?"http://".$_SERVER['SERVER_NAME']:"http://".$_SERVER['HTTP_HOST'];
        //echo $this->request->param();exit;
        
        //echo var_dump($this->params);exit;
        
        //file_put_contents('logfile.log', date("Y-m-d H:i:s"). " " . "ssss". "\r\n", FILE_APPEND | LOCK_EX);
        
        echo "ddd";
        exit;
        
        $devOrder=new DevOrder();
        $orderInfo=$devOrder->find(106);
        echo var_dump($orderInfo);
        exit;
    }
    
    /**
     * 订单装箱操作
     * @return type
     */
    public function orderPack (){  
        $retInfo=array("status"=>200,"msg"=>"提交成功！","data"=>array());
        //参数校验
        $result = $this->validate($this->params,'CommonValidate.save_cabinet');
        if($result!== true){
            // 验证失败 输出错误信息
            $retInfo=array("status"=>500,"msg"=>$result,"data"=>array());
        }

        $attachment=$this->params["attachment"];
        if(!is_array($this->params["attachment"])){
            $attachment= json_decode($this->params["attachment"],true);
        }

        if(empty($attachment)||!is_array($attachment)){
            $retInfo=array("status"=>500,"msg"=>"附件上传格式错误","data"=>array());
        }
        
        foreach ($attachment as $key=>$val){
            $attachInfo = $this->attachFile->find($val['id'])->toArray();
            $attachment[$key]["file_url"]=$attachInfo['file_url'];
        }
        $this->params["attachment"]=json_encode($attachment);
        $retInfo['data']=$this->devOrderPack->save($this->params);
        return json_api($retInfo);
    }
    
    
    /**
     * 图片上传公用接口
     * @return type
     */
    public function upload(){

        $file = request()->file("image");
        $info = $file->move('uploads');
        $retInfo=array("status"=>200,"msg"=>"上传成功！","data"=>array());
        if($info){
            $fileInfo=array(
                "file_name"=>$info->getFilename(),
                "file_url"=>"/wap/public/uploads/".str_replace('\\', '/', $info->getSaveName()),
                "file_type"=>$info->getExtension(),
            );  
            $fileId = $this->attachFile->save($fileInfo);
            $fileInfo["id"]=$this->attachFile->id;
            //$fileInfo=$this->attachFile->find($this->attachFile->id);
            $fileInfo["file_url"]=$this->request->domain().$fileInfo["file_url"];
            $retInfo["data"]=$fileInfo;
        }else{
            $retInfo=array("status"=>500,"msg"=>$file->getError(),"data"=>array());
        }
        return json_api($retInfo);
    }
    
}
